package com.zhs.gccs.activity.gccs;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.zhs.gccs.R;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.http.Api;
import com.zhs.gccs.http.ProgressObserver;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.vo.AdminPeopleAddBean;
import com.zhs.gccs.vo.AdminPeopleBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.RequestBody;

public class AdminPersonAddActivity extends BaseActivity {
    public static String intent_data = "data";
    @BindView(R.id.iv_back)
    View iv_back;
    @BindView(R.id.tv_save)
    View tv_save;
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_tel)
    EditText et_tel;
    @BindView(R.id.et_remarks)
    EditText et_remarks;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    AdminPeopleBean bean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_person_add);
//        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
//        ImmersionBar.with(this)
//                .statusBarColor(R.color.main_title_pressed_bg)
//                .statusBarDarkFont(false).init();
        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
        ButterKnife.bind(this);
        llRoot.setPadding(0, ImmersionBar.getStatusBarHeight(this), 0, 0);
        Intent intent = getIntent();
        if(intent.hasExtra(intent_data)){
            bean = (AdminPeopleBean) intent.getExtras().get(intent_data);
        }
        initView();
        if(bean!=null){
            et_name.setText(bean.getNickName());
            et_tel.setText(bean.getPhonenumber());
            et_remarks.setText(bean.getRemark());
        }
    }

    private void initView() {
        iv_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        tv_save.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                saveUser();
            }
        });
    }

    private void saveUser() {
        String name = et_name.getText().toString();
        if(TextUtils.isEmpty(name)){
            ToastUtils.toast("请输入姓名");
            return;
        }
        String tel = et_tel.getText().toString();
        if(TextUtils.isEmpty(tel)){
            ToastUtils.toast("请输入联系电话");
            return;
        }
        String remarks = et_remarks.getText().toString();
        if(TextUtils.isEmpty(remarks)){
            ToastUtils.toast("请输入工作单位");
            return;
        }
        showDialog();
        Map hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("tel", tel);
        hashMap.put("remarks", remarks);
        if(bean!=null){
            hashMap.put("userName", bean.getUserName());
        }
        RequestBody requestBody = createRequestBody(hashMap);
        toSubscribeSingleOb(Api.getDefault().saveGccsUser(requestBody),
                new ProgressObserver(this, false) {
                    @Override
                    protected void _onNext(String result) {
                        AdminPeopleAddBean bean = new Gson().
                                fromJson(result, AdminPeopleAddBean.class);
                        if (bean != null && bean.getCode() == 200) {
                            ToastUtils.toast("添加成功");
                            setResult(RESULT_OK);
                            finish();
                        }else {
                            ToastUtils.toast("添加失败，"+bean.getMsg());
                        }
                        dismissDialog();
                    }
                    @Override
                    protected void _onError(String result) {
                        ToastUtils.toast("添加失败");
                        dismissDialog();
                    }
                });
    }


}
